package controls

import (
	"github.com/labstack/echo/v4"
	"github.com/rs/xid"
	"math/rand"
	"sofuny/models"
	"sofuny/utils"
	"time"
)

// 随机一条
func RandomSentence(ctx echo.Context) error {
	rand.Seed(time.Now().UnixNano())
	var sentences []models.Sentence
	var resp SentenceResponse
	if err := db.Select("id, create_at, source, content, copy_counts").
		Where("status=?", true).
		Find(&sentences).Error; err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   "获取句子失败",
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	//// 随机一条句子
	sentence := sentences[rand.Intn(len(sentences))]
	// 赋值给响应
	resp.ID = sentence.ID
	resp.CreateAt = sentence.CreateAt.Format("2006-01-02 15:04:05")
	resp.Source = sentence.Source
	resp.Content = sentence.Content
	resp.CopyCounts = sentence.CopyCounts
	// 返回
	return ctx.JSON(200, utils.Response{
		Code:  200,
		Msg:   "success",
		Data:  resp,
		Total: 1,
		Time:  utils.FormatTime(),
	})
}

// 添加
func AddSentence(ctx echo.Context) error {
	var v utils.Value
	var sentence models.Sentence
	// 绑定数据
	if err := ctx.Bind(&v); err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   utils.DataBindErrorMessage,
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// 判断数据
	if v.NewSentence == "" || v.Source == "" {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   utils.DataEmptyMessage,
			Data:  nil,
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// value 赋值 sentence
	sentence.CreateAt = time.Now().Local()
	sentence.UpdateAt = sentence.CreateAt
	sentence.Status = true
	sentence.Uuid = xid.New().String()
	sentence.Source = v.Source
	sentence.Content = v.NewSentence

	// 创建
	if err := db.Create(&sentence).Error; err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   "创建句子失败",
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// 成功
	return ctx.JSON(200, utils.Response{
		Code:  200,
		Msg:   utils.DataSuccessMessage,
		Data:  v,
		Total: 1,
		Time:  utils.FormatTime(),
	})
}

// 添加复制数
func AddCopyCounts(ctx echo.Context) error {
	var v utils.Value
	var sentence models.Sentence
	// 绑定数据
	if err := ctx.Bind(&v); err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   utils.DataBindErrorMessage,
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// 查找句子
	if err := db.Where("status=? and id=?", true, v.ID).First(&sentence).Error; err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   "获取句子失败",
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// 添加复制数
	sentence.CopyCounts += 1
	sentence.UpdateAt = time.Now().Local()
	// 根据给定的条件更新单个属性
	if err := db.Save(&sentence).Error; err != nil {
		return ctx.JSON(200, utils.Response{
			Code:  201,
			Msg:   "更新复制数失败",
			Data:  err.Error(),
			Total: 0,
			Time:  utils.FormatTime(),
		})
	}
	// 成功
	return ctx.JSON(200, utils.Response{
		Code: 200,
		Msg:  utils.DataSuccessMessage,
		Data: map[string]int{
			"newCopyCounts": sentence.CopyCounts,
		},
		Total: 1,
		Time:  utils.FormatTime(),
	})
}
